﻿using System;
using System.Web.Script.Services;
using System.Web.Services;
using System.Reflection;
using System.Collections.Generic;
using Core;

/// <summary>
/// Summary description for Trip
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[ScriptService]
public class Kernel : WebService
{

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public object Process(object obj, string si  , string ps)
    {
        int startIndex = int.Parse(si);
        int sii = startIndex;
        if (startIndex > 0)
            sii = startIndex / int.Parse(ps);
        sii++;
        object[] pr = new object[2];
        pr[0] = obj;
        pr[1] = new { Result = "Fail", Records = (object)null, TotalRecordCount = 0 };
        Dictionary<string, object>  _ip = (Dictionary<string, object>)obj;
        _ip["si"] = sii;
        _ip["mr"] = int.Parse(ps);
        var d = new D();
        if (_ip.ContainsKey("a"))
        {
            var a = _ip["a"] as string;
            if (a != null && d._a.ContainsKey(a))
            {
                var _a = d._a[a];
                P p = new P();
                MethodInfo m = p.GetType().GetMethod(_a.T[5]);
                if (m != null)
                    m.Invoke(p, pr);// sử dụng gọi method
                else // call extension
                {
                    Type t = Type.GetType("Core.PE");
                    if (t != null)
                    {
                        object pe = Activator.CreateInstance(t);
                        m = pe.GetType().GetMethod(_a.T[5]);
                        if (m != null)
                            m.Invoke(p, pr);// sử dụng gọi method
                    }
                }
             }
        }
        return pr[1];
    }
    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public object P(object obj)
    {
        object[] pr = new object[2];
        pr[0] = obj;
        pr[1] = new { Result = "Fail", Records = "", TotalRecordCount = 0 };
        Dictionary<string, object> _ip = (Dictionary<string, object>)obj;
        var d = new D();
        if (_ip.ContainsKey("a"))
        {
            var a = _ip["a"] as string;
            if (a != null && d._a.ContainsKey(a))
            {
                var _a = d._a[a];
                P p = new P();
                MethodInfo m = p.GetType().GetMethod(_a.T[5]);
                if (m != null)
                    m.Invoke(p, pr);// sử dụng gọi method
                else // call extension
                {
                    Type t = Type.GetType("Core.PE");
                    if (t != null)
                    {
                        object pe = Activator.CreateInstance(t);
                        m = pe.GetType().GetMethod(_a.T[5]);
                        if (m != null)
                            m.Invoke(p, pr);// sử dụng gọi method
                    }
                }
            }
        }
        return pr[1];
    }
    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public object U(object obj)
    {
        object[] pr = new object[2];
        pr[0] = obj;
        pr[1] = new { Result = "Fail", Records = "", TotalRecordCount = 0 };
        Dictionary<string, object> _ip = (Dictionary<string, object>)obj;

        //-------------------------------------------------------------------
        //convert object
        var dd = new D();
        if (_ip.ContainsKey("id"))
        {
            int key = (int)_ip["id"];
            C[] f = D._fd[key];
            if (_ip.ContainsKey("d"))
            {
                var d = (Dictionary<string, object>)_ip["d"];
                var newd  = new  Dictionary<string, object>();
                for (int m = 0; m < f.Length; m++)
                {
                    string of = f[m].T[7];
                    newd[of]=d["f"+(m<=9?"0"+m.ToString():m.ToString()+"")];
                }
                _ip["d"] = newd;
            }
        }
        //------------------------------------------------------------------
        if (_ip.ContainsKey("a"))
        {
            var a = _ip["a"] as string;
            if (a != null && dd._a.ContainsKey(a))
            {
                var _a = dd._a[a];
                P p = new P();
                MethodInfo m = p.GetType().GetMethod(_a.T[5]);
                if (m != null)
                    m.Invoke(p, pr);// sử dụng gọi method
                else // call extension
                {
                    Type t = Type.GetType("Core.PE");
                    if (t != null)
                    {
                        object pe = Activator.CreateInstance(t);
                        m = pe.GetType().GetMethod(_a.T[5]);
                        if (m != null)
                            m.Invoke(p, pr);// sử dụng gọi method
                    }
                }
            }
        }
        return pr[1];
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public object R(object obj)
    {
        object[] pr = new object[2];
        pr[0] = obj;
        pr[1] = new { Result = "Fail", Records = "", TotalRecordCount = 0 };
        Dictionary<string, object> _ip = (Dictionary<string, object>)obj;

        //-------------------------------------------------------------------
        //convert object
        var dd = new D();
        if (_ip.ContainsKey("id"))
        {
            int key = (int)_ip["id"];
            C[] f = D._fd[key];
            if (_ip.ContainsKey("d"))
            {
                var d = (Dictionary<string, object>)_ip["d"];
                var newd = new Dictionary<string, object>();
                for (int m = 0; m < f.Length; m++)
                {
                    string of = f[m].T[7];
                    newd[of] = d["f" + (m <= 9 ? "0" + m.ToString() : m.ToString() + "")];
                }
                _ip["d"] = newd;
            }
        }
        //------------------------------------------------------------------
        if (_ip.ContainsKey("a"))
        {
            var a = _ip["a"] as string;
            if (a != null && dd._a.ContainsKey(a))
            {
                var _a = dd._a[a];
                P p = new P();
                MethodInfo m = p.GetType().GetMethod(_a.T[5]);
                if (m != null)
                {
                    m.Invoke(p, pr);// sử dụng gọi method
                    pr[1] = new { Result = "OK", Id = _ip["id"] , Name = _ip["n"] as string }; 
                }
                else // call extension
                {
                    Type t = Type.GetType("Core.PE");
                    if (t != null)
                    {
                        object pe = Activator.CreateInstance(t);
                        m = pe.GetType().GetMethod(_a.T[5]);
                        if (m != null)
                            m.Invoke(p, pr);// sử dụng gọi method
                    }
                }
            }
        }
        return pr[1];
    }
}
